package com.conan.controller;

import com.conan.pojo.Category;
import com.conan.pojo.Result;
import com.conan.service.CategoryService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
@Validated
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /*
    添加文章
     */
    @PostMapping
    public Result addcategory(@RequestBody @Validated(Category.Add.class) Category category) {
        categoryService.addCategory(category);
        return Result.success();
    }
    /*
        根据用户id查询文章列表
    */
    @GetMapping
    public Result<List<Category>> getCategoryList() {
        return Result.success(categoryService.getCategoryList());
    }
    /*
        根据文章id查询文章详细信息
    */
    @GetMapping("/detail")
    public Result<Category> getCategoryDetail(@RequestParam Integer id) {
        return Result.success(categoryService.getCategoryById(id));
    }
    /*
        修改文章详细信息
    */
    @PutMapping
    public Result updateCategory(@RequestBody @Validated(Category.Update.class) Category category) {
        categoryService.updateCategory(category);
        return Result.success();
    }
    /*
        删除文章
    */
    @DeleteMapping
    public Result deleteCategory(@RequestParam Integer id) {
        categoryService.deleteCategory(id);
        return Result.success();
    }

}
